﻿//  Symplectic Spark
//  Copyright 2010 Symplectic Ltd
//  Created by Martyn Whitwell (martyn@symplectic.co.uk)

//  This file is part of Spark.

//  Spark is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  Spark is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with Spark.  If not, see <http://www.gnu.org/licenses/>.

// $URL: https://symplectic-spark.googlecode.com/svn/trunk/website/App_Code/BaseType.cs $
// $LastChangedDate: 2010-03-02 15:01:50 +0000 (Tue, 02 Mar 2010) $
// $LastChangedRevision: 9 $
// $LastChangedBy: martyn@symplectic.co.uk $


using System;
using System.Xml.Linq;

namespace Symplectic.Spark
{
    /// <summary>
    /// Summary description for BaseType
    /// </summary>
    public class BaseType
    {
        public BaseType()
        { }

        public string Name { get; set; }


        #region ReadAttributeString
        protected string readAttributeString(XElement element, string attribute)
        {
            return readAttributeString(element, attribute, false);
        }

        protected string readAttributeString(XElement element, string attribute, bool recurseUp)
        {
            return readAttributeStringStatic(element, attribute, recurseUp);
        }

        protected static string readAttributeStringStatic(XElement element, string attribute, bool recurseUp)
        {
            if (element.Attribute(attribute) != null)
                return element.Attribute(attribute).Value;
            else
            {
                if (recurseUp && element.Parent != null)
                    return readAttributeStringStatic(element.Parent, attribute, recurseUp);
                else
                    return null;
            }
        }
        #endregion

        #region ReadAttributeInteger
        protected int readAttributeInteger(XElement element, string attribute)
        {
            return readAttributeInteger(element, attribute, false);
        }

        protected int readAttributeInteger(XElement element, string attribute, bool recurseUp)
        {
            return readAttributeIntegerStatic(element, attribute, recurseUp);
        }

        protected int readAttributeIntegerStatic(XElement element, string attribute, bool recurseUp)
        {
            if (element.Attribute(attribute) != null)
                return int.Parse(element.Attribute(attribute).Value);
            else
            {
                if (recurseUp && element.Parent != null)
                    return readAttributeIntegerStatic(element.Parent, attribute, recurseUp);
                else
                    return int.MinValue;
            }
        }
        #endregion


        public override string ToString()
        {
            return Name;
        }
    }
}